package com.qwc.blog.controller.back;

import com.qwc.blog.common.utils.FileUtils;
import com.qwc.blog.config.QblogProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.qwc.blog.common.constant.ResponseConstant.IMG_URL;

/**
 * @author qwc
 * @Date 2021/11/3 14:16
 */
@RestController
@RequestMapping("/blog/upload")
public class UploadController {

    @Autowired
    private QblogProperties qblogProperties;

    @RequestMapping("/md/picture")
    public ResponseEntity<Object> uploadMdPicture(@RequestParam(value = "file") final MultipartFile file) throws IOException {
        String picture = FileUtils.upload(file, qblogProperties.getPicturePath());
        Map<String, Object> map = new HashMap<>(4);
        map.put(IMG_URL, qblogProperties.getFileUrl() + picture);
        return ResponseEntity.ok(map);
    }

    @RequestMapping("/picture")
    public ResponseEntity<Object> uploadPicture(@RequestParam(value = "file") final MultipartFile file) throws IOException {
        String picture = FileUtils.upload(file, qblogProperties.getPicturePath());
        Map<String, Object> map = new HashMap<>(4);
        map.put(IMG_URL, picture);
        return ResponseEntity.ok(map);
    }
}
